package com.example.douyin;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.douyin.databinding.ActivityMainBinding;
import com.example.douyin.login.LoginViewModel;
import com.example.lib_base_mvvm.baseMVVM.BaseView.BaseActivity;
import com.example.lib_base_mvvm.entity.UserEntity;

@Route(path = "/app/MainActivity")
public class MainActivity extends BaseActivity<ActivityMainBinding, LoginViewModel> {
    @Autowired
    UserEntity.DataBean user;

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        if(user!=null){
            dataBinding.etUsername.setText(user.getUsername());
            dataBinding.etPassword.setText(user.getPassword());
        }
        dataBinding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.login(dataBinding.etUsername.getText().toString(),dataBinding.etPassword.getText().toString());
            }
        });
        String string = dataBinding.cb.getText().toString();
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(string);
        stringBuilder.setSpan(new ForegroundColorSpan(getColor(R.color.teal_200)),6,12, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        stringBuilder.setSpan(new ForegroundColorSpan(getColor(R.color.teal_200)),13,19,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ClickableSpan span = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                ToastUtils.showLong("我阅读了");
                //跳转到具体的页面中，加载内容
                ARouter.getInstance().build("/app/WebActivity").navigation();
            }
        };
        stringBuilder.setSpan(span,6,12,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        dataBinding.cb.setMovementMethod(LinkMovementMethod.getInstance());//不添加这行，点击事件不能生效
        dataBinding.cb.setText(stringBuilder);

    }

    @Override
    public void initData() {

        viewModel.success.observe(this, new Observer<UserEntity>() {
            @Override
            public void onChanged(UserEntity entity) {
                if(entity.getCode()==200){
                    //保存相关信息
                    SPUtils.getInstance().put("token",entity.getData().getToken());
                    SPUtils.getInstance().put("username",entity.getData().getUsername());
                    SPUtils.getInstance().put("password",entity.getData().getPassword());
                    SPUtils.getInstance().put("icon",entity.getData().getIcon());
                    ToastUtils.showLong(entity.getMessage());
                    //ARouter.getInstance().build("/app/DouActivity").navigation();
                    viewModel.loginIM(entity.getData().getUsername(),entity.getData().getPassword());
                }else{
                    //跳转到注册界面
                    ARouter.getInstance().build("/app/RegisterActivity").withString("name",dataBinding.etUsername.getText().toString()).navigation();
                }
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });

        viewModel.imSuccess.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
                ARouter.getInstance().build("/app/DouActivity").navigation();
            }
        });
        viewModel.imFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong("环信"+s);
            }
        });

    }

    @Override
    public Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }
}